using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OpenComposite.EII.Designer.Workflow.Forms
{
	public partial class FormCreateMashupControl : Form
	{
		#region Initialization
		public FormCreateMashupControl(Repository.Workflow workflow, MashupUIConfigType configType)
		{
			_workflow = workflow;
			_configType = configType;

			_existingNames = new List<string>(workflow.ExtendedProperties.MashupControls.Keys);

			this.DialogResult = DialogResult.Cancel;

			InitializeComponent();

			cmbType.BeginUpdate();
			cmbType.Items.Clear();
			if ( configType == MashupUIConfigType.Event ) {
				cmbType.Items.AddRange((object[])MashupStatics.TypesEvent.ToArray());
			} else {
				cmbType.Items.AddRange((object[])MashupStatics.TypesMethod.ToArray());
			}
			cmbType.EndUpdate();
		}
		#endregion

		#region Public Members

		#region Properties
		public string ControlName
		{
			get { return txtName.Text; }
		}
		public Type ControlType
		{
			get { return cmbType.SelectedItem as Type; }
		}
		#endregion

		#endregion

		#region Private Members

		#region Fields
		private Repository.Workflow _workflow;
		private List<string> _existingNames;
		private MashupUIConfigType _configType;
		#endregion

		#region Event Handlers
		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			if ( _existingNames.Contains(txtName.Text) ) {
				DialogResult result = MessageBox.Show(string.Format(
					"A control with the name '{0}' exists.{1}{1}Please enter another name or " +
					"press <Cancel> if you don't want to create a new control.", txtName.Text, Environment.NewLine),
					this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
				if ( result == DialogResult.Cancel ) {
					this.DialogResult = DialogResult.Cancel;
					this.Close();
					return;
				} else {
					txtName.Focus();
					txtName.SelectAll();
					return;
				}
			}
			if ( cmbType.SelectedIndex == -1 ) {
				MessageBox.Show("Please select the type of the new control.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
				cmbType.Focus();
				return;
			}
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
		#endregion

		#endregion
	}

}